#import "YoukeViewController.h"
#import "collectionCell.h"
#import "WXViewController.h"
#import "WeixListViewController.h"
#import "YpViewController.h"
#import "WyHomeListViewController.h"
#import "TongzhiListViewController.h"
#import "Reg1ViewController.h"
#import "LoginViewController.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"

@interface YoukeViewController ()
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) NSMutableArray *tzList;
@end

@implementation YoukeViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    [self setUpCollection];
    [self loadWeather];

    NSString *image1Str = [NSString stringWithFormat:@"%@%@",SERVER_PATH,@"home_photo.png"];
    NSURL *url1 = [NSURL URLWithString:image1Str];
    [_touxiangImage sd_setImageWithURL:url1];
    
    if (self.networkManager == nil) {
        self.networkManager = [[NetworkManager alloc] init];
    }
    NSString *xiaoqu_id = @"8";
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           xiaoqu_id,@"xiaoqu_id", nil];
    [self.networkManager requestWithMethod:zhiding parameter:param delegate:self];
    [self.HUD show:YES];

}

- (void)loadWeather
{
    NSURL *URL =[NSURL URLWithString:@"http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101190701&weatherType=0"];
    NSError *error;
    NSString *stringFromFileAtURL = [[NSString alloc]
                                     initWithContentsOfURL:URL
                                     encoding:NSUTF8StringEncoding
                                     error:&error];
    NSString *strTemp;
    NSString *date_y;
    NSString *strWeather;
    NSString *city;
    NSString *fileName=@"晴.png";
    if(stringFromFileAtURL !=nil)
    {
        NSArray *strarray = [stringFromFileAtURL componentsSeparatedByString:@"\""];
        
        for(int i=0;i<strarray.count;i++)
        {
            NSString *str = [strarray objectAtIndex:i];
            if(YES == [str isEqualToString:@"city"])//时间
            {
                city = [strarray objectAtIndex:i+2];
            }
            else if(YES == [str isEqualToString:@"date_y"])//时间
            {
                date_y = [strarray objectAtIndex:i+2];
            }
            else if(YES == [str isEqualToString:@"temp1"])//最低温度_最高温度
            {
                strTemp = [strarray objectAtIndex:i+2];
            }
            else if(YES == [str isEqualToString:@"weather1"])//天气
            {
                strWeather = [strarray objectAtIndex:i+2];
            }
            
        }
        self.cityText.text = city;
        self.dayText.text = date_y;
        self.wenduText.text = strTemp;
        NSString *sweather = [[NSString alloc]initWithFormat:@"%@",strWeather];
        
        
        if(NSNotFound != [strWeather rangeOfString:@"晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@",@"晴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"多云"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"多云.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"小雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雷"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雾"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雾.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"中雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"中雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"小雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"小雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"小雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雨加雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雨夹雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雷阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷阵雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雨转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨转晴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阴转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴转晴.png"];
        }
        
        if(sweather !=nil)
            self.weathText.text = sweather;
    }
    NSLog(fileName);
    self.weathImage.image = [UIImage imageNamed:fileName];
    
}



-(void)setUpCollection{
    self.dataMArr = [NSMutableArray array];
    for(NSInteger index = 0;index<7; index++){
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",(long)index+1]];
        NSString *title = NULL;
        if(index == 0){
            title =  @"物业通知" ;
        } else if(index == 1){
            title =  @"小区黄页" ;
        } else if(index == 2){
            title =  @"邻里小圈" ;
        } else if(index == 3){
            title =  @"维修业务" ;
        } else if(index == 4){
            title =  @"小区缴费" ;
        } else if(index == 5){
            title =  @"小区商圈" ;
        } else if(index == 6){
            title =  @"物业之家" ;
        }
        
        NSDictionary *dic = @{@"image": image, @"title":title};
        [self.dataMArr addObject:dic];
    }
    self.myConllection.delegate = self;
    self.myConllection.dataSource = self;
    
    
}

- (IBAction)toRegction:(id)sender {
    Reg1ViewController *problemVC = (Reg1ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"Reg1ViewController"];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
    DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
     [self.navigationController pushViewController:menuVC animated:YES];
}

- (IBAction)toLogin:(id)sender {
    LoginViewController *findVC = (LoginViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:findVC animated:NO];
}

#pragma mark - Collection View Data Source
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataMArr.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
//        TongzhiListViewController *problemVC = (TongzhiListViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"TongzhiListViewController"];
//        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
//        DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//        [self presentViewController:menuVC animated:YES completion:^{}];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"游客无法查看此功能，请登录帐号查看"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];

    } else if(indexPath.row == 1){
        YpViewController *problemVC = (YpViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"YpViewController"];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
        DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//        [self presentViewController:menuVC animated:YES completion:^{}];
         [self.navigationController pushViewController:menuVC animated:YES];
    } else if(indexPath.row == 2){
        WXViewController *wxVc = [WXViewController new];
//        [self presentViewController:wxVc animated:YES completion:NULL];
        [self.navigationController pushViewController:wxVc animated:YES];
            } else if(indexPath.row == 3){
//        WeixListViewController *problemVC = (WeixListViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WeixListViewController"];
//        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
//        DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//        [self presentViewController:menuVC animated:YES completion:^{}];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"游客无法查看此功能，请登录帐号查看"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];

    } else if(indexPath.row == 4){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"此功能尚未开放"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    } else if(indexPath.row == 5){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"此功能尚未开放"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    } else if(indexPath.row == 6){
        WyHomeListViewController *problemVC = (WyHomeListViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WyHomeListViewController"];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
        DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//        [self presentViewController:menuVC animated:YES completion:^{}];
         [self.navigationController pushViewController:menuVC animated:YES];
    }
    return;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *collectionCellID = @"myCollectionCell";
    collectionCell *cell = (collectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
    
    NSDictionary *dic    = self.dataMArr[indexPath.row];
    UIImage *image       = dic[@"image"];
    NSString *title      = dic[@"title"];
    
    cell.imageView.image = image;
    cell.titleLabel.text = title;
    return cell;
};

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    if (methodId == zhiding) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            
            if([result isEqualToString:@"000"]){
                
                NSArray *results = [data objectForKeyOrNil:@"zhidingList"];
                NSMutableArray* titles = [[NSMutableArray alloc] init];
                NSMutableArray* imagesURLStrings = [[NSMutableArray alloc] init];
                
                if (![results isKindOfClass:[NSNull class]] && [results count] > 0) {
                    [self.tzList  addObjectsFromArray:results];
                    for(int i=0;i<[results count];i++){
                        NSMutableDictionary *str = [results objectAtIndex:i];
                        [titles addObject:[[str objectForKey:@"title"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                        [imagesURLStrings addObject:@"beijing1.jpg"];
                    }
                    CGFloat w = self.view.bounds.size.width;
                    //网络加载 --- 创建带标题的图片轮播器
                    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 180, w, 55) imageURLStringsGroup:nil]; // 模拟网络延时情景
                    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
                    cycleScrollView2.titlesGroup = titles;
//                    cycleScrollView2.dotColor = [UIColor grayColor]; // 自定义分页控件小圆标颜色
                    cycleScrollView2.placeholderImage = [UIImage imageNamed:@"placeholder"];
                    [self.view addSubview:cycleScrollView2];
                    //             --- 模拟加载延迟
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        cycleScrollView2.imageURLStringsGroup = imagesURLStrings;
                    });
                }
            } 
        }
        
    }
}


@end

